package cn.dckeji.dcssm.service.impl;

import cn.dckeji.dcssm.common.enums.ProjectEnum;
import cn.dckeji.dcssm.dao.AdminDao;
import cn.dckeji.dcssm.dao.MenuDao;
import cn.dckeji.dcssm.dao.RoleDao;
import cn.dckeji.dcssm.dao.RoleMenuDao;
import cn.dckeji.dcssm.entity.Menu;
import cn.dckeji.dcssm.entity.Role;
import cn.dckeji.dcssm.entity.RoleMenu;
import cn.dckeji.dcssm.service.RoleService;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Kenny
 * @since 2019-02-26
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

    @Resource
    private RoleDao roleDao;
    @Resource
    private AdminDao adminDao;
    @Resource
    private MenuDao menuDao;
    @Resource
    private RoleMenuDao roleMenuDao;

    @Override
    public void deleteById(String roleId) {
        //删除角色下的管理员
        Map map1 = new HashMap();
        map1.put("role_id", roleId);
        adminDao.deleteByMap(map1);
        //删除角色和菜单的关联关系
        Map map2 = new HashMap();
        map2.put("role_id", roleId);
        roleMenuDao.deleteByMap(map2);
        roleDao.deleteById(roleId);
    }

    @Override
    public JSONObject getRoleMenu(String roleId) {

        JSONObject jsonObject = new JSONObject();
        Map status = new HashMap();
        status.put("code", 200);
        status.put("message", "操作成功");
        jsonObject.put("status", status);

        List<String> roleMenuIdlist = new ArrayList<>();
        if (StringUtils.isNotBlank(roleId)) {
            //查询这个角色对应的所有菜单
            List<RoleMenu> roleMenuList = roleMenuDao.selectList(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
            if (CollectionUtil.isNotEmpty(roleMenuList)) {
                for (RoleMenu rm:roleMenuList) {
                    String rmMenuId = rm.getMenuId();
                    roleMenuIdlist.add(rmMenuId);
                }
            }
        }

        //查询所有一级菜单
        List<Menu> menuList = menuDao.selectList(new QueryWrapper<Menu>().eq("pid", ProjectEnum.MENU_PID_DEFAULT.code()));
        List<Map> menuMapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(menuList)) {
            for (Menu m:menuList) {
                Map<String, Object> stringObjectMap = new HashMap<>();
                String menuId = m.getId();
                if (roleMenuIdlist.contains(menuId)) {
                    List<Map> alist = new ArrayList<>();
                    Map map = new HashMap();
                    map.put("type",0);
                    map.put("isChecked", 1);
                    alist.add(map);
                    stringObjectMap.put("checkArr", alist);
                }else {
                    List<Map> alist = new ArrayList<>();
                    Map map = new HashMap();
                    map.put("type",0);
                    map.put("isChecked", 0);
                    alist.add(map);
                    stringObjectMap.put("checkArr", alist);
                }
                stringObjectMap.put("title", m.getTitle());
                stringObjectMap.put("parentId", "-1");
                stringObjectMap.put("level", 1);
                stringObjectMap.put("id", m.getId());

                //查询二级菜单
                List<Menu> kidMenuList = menuDao.selectList(new QueryWrapper<Menu>().eq("pid", menuId));

                List<Map> kidMenuMapList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(kidMenuList)) {
                    stringObjectMap.put("isLast",false);

                    for (Menu kidm:kidMenuList) {
                        Map<String, Object> kidMap = new HashMap<>();
                        String kidmId = kidm.getId();
                        if (roleMenuIdlist.contains(kidmId)) {
                            List<Map> alist = new ArrayList<>();
                            Map map = new HashMap();
                            map.put("type",0);
                            map.put("isChecked", 1);
                            alist.add(map);
                            kidMap.put("checkArr", alist);
                        }else {
                            List<Map> alist = new ArrayList<>();
                            Map map = new HashMap();
                            map.put("type",0);
                            map.put("isChecked", 0);
                            alist.add(map);
                            kidMap.put("checkArr", alist);
                        }

                        kidMap.put("title", kidm.getTitle());
                        kidMap.put("parentId", kidm.getPid());
                        kidMap.put("level", 2);
                        kidMap.put("isLast",true);
                        kidMap.put("id", kidm.getId());

                        kidMenuMapList.add(kidMap);
                    }
                }else {
                    stringObjectMap.put("isLast",true);
                }
                stringObjectMap.put("children", kidMenuMapList);
                menuMapList.add(stringObjectMap);
            }
        }
        jsonObject.put("data", menuMapList);
        return jsonObject;
    }

    @Override
    public void save(String roleId, String menuIds, String roleName) {
        String roleIdstring;
        if (StringUtils.isNotBlank(roleId)) {
            //删除原来的菜单
            Map map = new HashMap();
            map.put("role_id", roleId);
            roleMenuDao.deleteByMap(map);
            Role role = roleDao.selectById(roleId);
            role.setGmtModified(LocalDateTime.now());
            role.setRoleName(roleName);
            roleIdstring = roleId;
        }else {
            Role role = new Role();
            role.setRoleName(roleName);
            role.setGmtCreate(LocalDateTime.now());
            roleDao.insert(role);
            roleIdstring = role.getId();
        }
        //插入新的菜单
        if (StringUtils.isNotBlank(menuIds)) {
            String[] split = menuIds.split(",");
            for (int i = 0; i < split.length; i++) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setGmtCreate(LocalDateTime.now());
                roleMenu.setMenuId(split[i]);
                roleMenu.setRoleId(roleIdstring);
                roleMenuDao.insert(roleMenu);
            }
        }
    }
}
